from time import sleep
from selenium.webdriver.common.keys import Keys

from config import userInfo

from element import findCssEle, findCssEles


def login(driver, first=True):
    if first:
        account = driver.find_element_by_id("account")
        account.clear()
        account.send_keys(userInfo.get('account'))
        password = driver.find_element_by_id("password")
        password.clear()
        password.send_keys(userInfo.get('password'))

    # 输入验证码
    manuCode = input('please input code:')
    code = driver.find_element_by_id("code")
    code.send_keys(manuCode)
    code.send_keys(Keys.RETURN)

    sleep(1)

    try:
        findCssEle(driver, '.ant-modal-body button')
        login(driver, False)
        print(1)
    except:
        print(2)
        pass


def logOut(driver):
    # log out
    findCssEle(driver, '.ant-dropdown-trigger')
    findCssEles(driver, '.ant-dropdown-menu-item', 2)
    sleep(1)
    findCssEles(driver, '.ant-modal-confirm-btns button', 1)
    pass
